package com.ruoyi.wbs.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wbs.domain.WbsPlan;
import com.ruoyi.wbs.service.IWbsPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * WBS计划Controller
 * 
 * @author zhaogang
 * @date 2024-07-18
 */
@RestController
@RequestMapping("/wbs/wbsPlan")
public class WbsPlanController extends BaseController
{
    @Autowired
    private IWbsPlanService wbsPlanService;

    /**
     * 查询WBS计划列表
     */
    @PreAuthorize("@ss.hasPermi('wbs:wbsPlan:list')")
    @GetMapping("/list")
    public TableDataInfo list(WbsPlan wbsPlan)
    {
        startPage();
        List<WbsPlan> list = wbsPlanService.selectWbsPlanList(wbsPlan);
        return getDataTable(list);
    }

    /**
     * 导出WBS计划列表
     */
    @PreAuthorize("@ss.hasPermi('wbs:wbsPlan:export')")
    @Log(title = "WBS计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WbsPlan wbsPlan)
    {
        List<WbsPlan> list = wbsPlanService.selectWbsPlanList(wbsPlan);
        ExcelUtil<WbsPlan> util = new ExcelUtil<WbsPlan>(WbsPlan.class);
        util.exportExcel(response, list, "WBS计划数据");
    }

    /**
     * 获取WBS计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('wbs:wbsPlan:query')")
    @GetMapping(value = "/{wbsId}")
    public AjaxResult getInfo(@PathVariable("wbsId") Long wbsId)
    {
        return success(wbsPlanService.selectWbsPlanByWbsId(wbsId));
    }

    /**
     * 新增WBS计划
     */
    @PreAuthorize("@ss.hasPermi('wbs:wbsPlan:add')")
    @Log(title = "WBS计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WbsPlan wbsPlan)
    {
        return toAjax(wbsPlanService.insertWbsPlan(wbsPlan));
    }

    /**
     * 修改WBS计划
     */
    @PreAuthorize("@ss.hasPermi('wbs:wbsPlan:edit')")
    @Log(title = "WBS计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WbsPlan wbsPlan)
    {
        return toAjax(wbsPlanService.updateWbsPlan(wbsPlan));
    }

    /**
     * 删除WBS计划
     */
    @PreAuthorize("@ss.hasPermi('wbs:wbsPlan:remove')")
    @Log(title = "WBS计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wbsIds}")
    public AjaxResult remove(@PathVariable Long[] wbsIds)
    {
        return toAjax(wbsPlanService.deleteWbsPlanByWbsIds(wbsIds));
    }

    /**
     * 下载
     */
@PostMapping("/download")
    public void download(String pic, HttpServletResponse response) throws IOException {
        //获取路径, 从第二个"/"开始获取
        String path = pic.substring( pic.indexOf("/",1));
        //找到文件
        File file = new File(RuoYiConfig.getProfile() + path);
        //判断文件是否存在
        if (file.exists()) {
            //开始下载
            //编码类型
            response.setCharacterEncoding("UTF-8");
            //设置响应类型     二进制流
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            //设置头信息                                          附件方式     文件名
            response.setHeader("Content-disposition", "attachment; filename=t" ); 

            //文件的输入流
            FileInputStream in = new FileInputStream(file);
            //响应的输出流
            ServletOutputStream out = response.getOutputStream();

            //一次读1k 设置一个读取文件的缓冲区
            byte[] buffer = new byte[1024];
            int len ;
            //循环读取文件内容 一次1k
            while ((len = in.read(buffer)) > 0 ) {
                //把读到的信息放入输出流
                out.write(buffer, 0, len);
            }

            //关闭输入流
            IOUtils.close(in);
            //关闭输出流
            IOUtils.close(out);

        }
    }

}
